import os

from paste import httpexceptions
from paste.cascade import Cascade
from paste.urlparser import StaticURLParser, make_static
from paste.registry import RegistryManager
from paste.deploy.config import ConfigMiddleware
from paste.recursive import RecursiveMiddleware
from paste.urlmap import URLMap

from beaker.middleware import CacheMiddleware

here = os.path.abspath(os.path.dirname(__file__))

# configuration dicts

#this was just taken over from the paste conf
global_conf = {
    'port' : 7100,
    'host' : '0.0.0.0',
}

app_conf = {
    'static_files' : os.path.join(here, 'static'),
    'weblog_base_url' : 'http://sonicradar.com/blog/',
    'main_xslt' : 'static/ionrock/index.xslt',
    'template_files' : 'static',
    'entries_per_page' : 5,
    'repository_type' : 'flatfile',
    'flatfile_storedir' : os.path.join(here, 'entries'),
    'atom_default_collection' : 'entries',
    'atom_service_doc' : os.path.join(here, 'service.xml')
}


from brightcontent.core.store import flatfile_repository
store = flatfile_repository(**app_conf)

from brightcontent.core.application import WsgiApplication
from brightcontent.core.renderer import FeedRenderer
app = WsgiApplication(None, store, app_conf)
app = FeedRenderer(app, app_conf)

# Expose config variables to other plugins
app = ConfigMiddleware(app, {'app_conf':app_conf,
        'global_conf':global_conf})

# Main place for plug-ins
# Put your own middleware here, so that any problems are caught by the error
# handling middleware

# BC Comments - Comments plugin for BC that uses an BC store
#from brightcontent.plugins.comments import CommentForms
#from brightcontent.core.store import flatfile_repository
#app = CommentForms(app, store, app_conf)

# BC Archives - Archives plugin that will display a list of
# year/months to see past posts
from brightcontent.plugins.archives import ArchiveLinks
app = ArchiveLinks(app, store, app_conf)

# BC Weblog Calendar - Adds a calendar for the archives
from brightcontent.plugins.archives import WeblogCalendar
app = WeblogCalendar(app, store, app_conf)

# Caching support from Beaker 
app = CacheMiddleware(app, global_conf)

# Change HTTPExceptions to HTTP responses 
app = httpexceptions.make_middleware(app, global_conf)
    
# Error Handling 
#app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware)
    
# Serve static files from the specified directory 
static_app = StaticURLParser(app_conf['static_files'])

urlmap = URLMap()
urlmap['/static'] = static_app
urlmap['/'] = app

# Permits recursive calls into the middleware stack, to save overhead
# for internal and forwarded requests
app = RecursiveMiddleware(urlmap, global_conf)

#static_app = StaticURLParser(config.paths['static_files'])

import cherrypy

class Root:
    pass

if __name__ == '__main__':
    global_conf = {'engine.autoreload_on' : True,
                   'server.socket_port' : 7100, }
    cherrypy.config.update({ 'global' : global_conf })
    conf = {'/' : { 'tools.wsgiapp.on' : True,
                    'tools.wsgiapp.app' : app,
                    'tools.gzip.on' : True,
                    'tools.trailing_slash.on' : True }}
    cherrypy.tree.mount(Root(), '/', config=conf)
    cherrypy.server.quickstart()
    try:
        cherrypy.engine.start()
    except KeyboardInterrupt:
        cherrypy.engine.stop()
